/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.edu.cs576.HyperLinkVideo.authoringtool;

import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.image.BufferedImage;
import java.util.ArrayList;
import java.util.List;
import javax.swing.JMenu;
import javax.swing.JPanel;

/**
 *
 * @author Ludo
 */
public class SecondaryVideoViewPanel extends JPanel {

    private BufferedImage image;
    private int width = 352;
    private int height = 288;
    private byte[] videoBytes; //the bytes that will currently be shown on the panel
    private int totalFrames;
    private int currentSecondaryVideo;
    private List<byte[]> secondaryVideos;
    private List<String> secondaryVideoFilenames;
    
    
    public SecondaryVideoViewPanel(){
        super();
        currentSecondaryVideo = -1;
        secondaryVideos = new ArrayList<byte[]>();
        secondaryVideoFilenames = new ArrayList<String>();
    }

    protected void paintComponent(Graphics g) {
        super.paintComponent(g);

        if (image != null) {
            g.clearRect(0, 0, width, height);
            g.drawImage(image, 0, 0, null);
            g.dispose();
        }

    }
    
    public String getCurrentVideoName(){
        return secondaryVideoFilenames.get(currentSecondaryVideo);
    }
    public int getCurrentVideoId(){
        return currentSecondaryVideo;
    }
    
    /**
     * Setting byte array of video to be displayed
     *
     * @param bytes
     */
    public void setBytes(byte[] bytes) {
        videoBytes = bytes;
        totalFrames = videoBytes.length/height/width/3-1;
        initImage();
    }
    
    public void addSecondaryVideoToList(String filename , byte[] bytes){
        //System.out.println("Current Secondary Video: "+filename);
        secondaryVideoFilenames.add(filename);
        secondaryVideos.add(bytes);
    }
    public List<String> getSecondaryVideoFilenames(){
        return secondaryVideoFilenames;
    }
    /**
     * Used for assigning secondary videos from the drop down menu
     * (they've already been saved in the list of bytes).
     * @param index 
     */
    public void setVideo(int index){
        //System.out.println("Current Secondary Video: "+secondaryVideoFilenames.get(index));
        currentSecondaryVideo = index;
        setBytes(secondaryVideos.get(index));
    }
    
    public void removeSecondaryVideo(int index, JMenu jMenu){
        if(secondaryVideos.size()>0){
            jMenu.remove(index+1);//remove menu item
            secondaryVideos.remove(index);//bytes from bytes list
            secondaryVideoFilenames.remove(index);
            videoBytes = null;
        }
    }
    
    public void removeOldestSecondaryVideo(JMenu jMenu){
        removeSecondaryVideo(0, jMenu); // removing video from videos list and menu items
        //update menu item indexes
        //Component[] components = jMenu.get.getComponents();
        //starts at 1, because 0 is always the "from filesystem menu item"
        for(int i = 1; i < jMenu.getItemCount(); i++){
            CustomMenuItem menuItem = (CustomMenuItem)jMenu.getItem(i);
            menuItem.setIndex(menuItem.getIndex()-1);
        }
        
    }
    
    public int getTotalNumberOfFrames(){
        if(videoBytes != null){
            return totalFrames;
        }else{
            return 0;
        }
    }
    
    public boolean isVideoSet(){
        if (videoBytes == null){
            return false;
        }else{
            return true;
        }
    }
    
    public void setFrame(int frameNum){
        image = getFrame(frameNum);
        this.repaint();
    }

    /**
     * Picks frame to display of current video in jPanel. Make sure videoBytes
     * is not null.
     */
    private BufferedImage getFrame(int frame) {
        BufferedImage img = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
        if (videoBytes == null) {
            System.out.println("video bytes is null.");
            return img;
        }
        int ind = frame * height * width * 3;
        for (int y = 0; y < height; y++) {

            for (int x = 0; x < width; x++) {

                byte a = 0;
                byte r = videoBytes[ind];
                byte g = videoBytes[ind + height * width];
                byte b = videoBytes[ind + height * width * 2];

                int pix = 0xff000000 | ((r & 0xff) << 16) | ((g & 0xff) << 8) | (b & 0xff);
                //int pix = ((a << 24) + (r << 16) + (g << 8) + b);
                img.setRGB(x, y, pix);
                ind++;

            }
        }
        return img;
    }

    private void initImage() {
        image = getFrame(0);
        Graphics2D g2 = (Graphics2D) image.getGraphics();
    }
    
}
